<template>
  <div class="recommendations-container">
    <div class="header">
      <h1>为您精心推荐</h1>
      <el-text class="inspiration">{{ randomInspiration }}</el-text>
    </div>
    
    <el-divider />
    
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="5" animated />
    </div>
    
    <div v-else>
      <el-empty v-if="recommendations.length === 0" description="暂无推荐" />
      
      <div v-else class="recommendations-list">
        <el-card 
          v-for="attraction in recommendations" 
          :key="attraction.id"
          class="recommendation-card"
          @click="viewDetail(attraction.id)"
        >
          <div class="card-content">
            <div class="image-placeholder">
              <el-icon :size="60" color="#409EFF"><Picture /></el-icon>
            </div>
            <div class="info">
              <h3>{{ attraction.name }}</h3>
              <el-text type="info" class="address">
                <el-icon><Location /></el-icon>
                {{ attraction.address }}
              </el-text>
              <div class="meta">
                <el-tag v-if="attraction.cost" type="success">{{ attraction.cost }}</el-tag>
                <el-text type="info">{{ attraction.open_time || '开放时间待定' }}</el-text>
              </div>
              <el-text class="description">{{ attraction.description || '暂无描述' }}</el-text>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    
    <div class="footer-actions">
      <el-button 
        type="primary" 
        @click="refreshRecommendations"
        :loading="loading"
      >
        <el-icon><Refresh /></el-icon>
        换一批
      </el-button>
      <el-button @click="goBack">
        <el-icon><Back /></el-icon>
        返回地图
      </el-button>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  Picture, 
  Location, 
  Refresh,
  Back
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'  // 引入消息组件
import request from '@/utils/request.js'

export default {
  name: 'RecommendationsPage',
  components: {
    Picture,
    Location,
    Refresh,
    Back
  },
  setup() {
    const router = useRouter()
    const recommendations = ref([])
    const loading = ref(true)
    const randomInspiration = ref('')
    
    // 励志短语集合
    const inspirations = [
      "旅途不在于目的地，而在于沿途的风景",
      "生活不是等待风暴过去，而是学会在雨中跳舞",
      "最美的风景总在最崎岖岖的路上",
      "旅行是唯一让你付出却更富有的投资",
      "世界那么大，你该去看看",
      "每一次出发都是新的开始",
      "旅行是心灵的阅读，阅读是心灵的旅行",
      "人生没有地图，只能用心去感受",
      "最美的回忆往往来自最意外的旅程",
      "不要只计划，要行动！"
    ]
    
    // 获取推荐景点
    const fetchRecommendations = async () => {
      try {
        loading.value = true
        
        // 从localStorage获取用户ID
        const userInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
        const userId = userInfo.id || 'default_user'
        
        // 调用推荐接口
        const response = await request.get('/recommend_attractions', {
          params: { user_id: userId }
        })
        
        // 处理响应数据
        const data = response.data || {}
        recommendations.value = data['推荐景点'] || []
        
        // 如果没有推荐则使用热门景点
        if (recommendations.value.length === 0) {
          recommendations.value = [
            {"id": "B000A8Q", "name": "故宫博物院", "address": "北京市东城区景山前街4号", "description": "中国明清两代的皇家宫殿，旧称紫禁城", "cost": "60元", "open_time": "08:30-17:00"},
            {"id": "B000A8R", "name": "长城", "address": "北京市延庆区八达岭长城", "description": "中国古代伟大的防御工程，世界文化遗产", "cost": "40元", "open_time": "06:00-19:00"},
            {"id": "B000A8S", "name": "颐和园", "address": "北京市海淀区新建宫门路19号", "description": "中国清朝时期皇家园林，前身为清漪园", "cost": "30元", "open_time": "06:30-20:00"},
            {"id": "B000A8T", "name": "西湖", "address": "浙江省杭州市西湖区龙井路1号", "description": "中国大陆首批国家重点风景名胜区", "cost": "免费", "open_time": "全天"},
            {"id": "B000A8U", "name": "黄山", "address": "安徽省黄山市黄山区汤口镇", "description": "世界文化与自然双重遗产，中华十大名山", "cost": "230元", "open_time": "06:00-18:00"}
          ]
        }
        
        // 随机选择励志短语
        randomInspiration.value = inspirations[Math.floor(Math.random() * inspirations.length)]
        
      } catch (error) {
        console.error('获取推荐失败:', error)
        // 使用Element Plus消息组件显示错误
        ElMessage({
          type: 'error',
          message: '获取推荐失败，请稍后重试',
          showClose: true,
          duration: 3000
        })
      } finally {
        loading.value = false
      }
    }
    
    // 查看详情
    const viewDetail = (id) => {
      router.push(`/attraction/${id}`)
    }
    
    // 刷新推荐
    const refreshRecommendations = () => {
      fetchRecommendations()
    }
    
    // 返回地图
    const goBack = () => {
      router.push('/components/AMap')
    }
    
    onMounted(fetchRecommendations)
    
    return {
      recommendations,
      loading,
      randomInspiration,
      viewDetail,
      refreshRecommendations,
      goBack
    }
  }
}
</script>

<style scoped>
.recommendations-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 20px;
}

.header {
  text-align: center;
  margin-bottom: 20px;
}

.header h1 {
  color: var(--el-color-primary);
  margin-bottom: 10px;
}

.inspiration {
  font-size: 18px;
  font-style: italic;
  color: var(--el-color-success);
  display: block;
}

.loading-container {
  padding: 50px 0;
}

.recommendations-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 20px;
}

.recommendation-card {
  cursor: pointer;
  transition: all 0.3s ease;
  height: 100%;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--el-box-shadow-light);
}

.recommendation-card:hover {
  transform: translateY(-5px);
  box-shadow: var(--el-box-shadow);
}

.card-content {
  display: flex;
  gap: 20px;
  padding: 15px;
}

.image-placeholder {
  flex-shrink: 0;
  width: 120px;
  height: 120px;
  background-color: var(--el-color-primary-light-9);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--el-border-color);
}

.info {
  flex: 1;
}

.info h3 {
  margin-top: 0;
  margin-bottom: 10px;
  color: var(--el-text-color-primary);
  font-size: 18px;
}

.address {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 10px;
  font-size: 14px;
}

.meta {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
}

.description {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  color: var(--el-text-color-regular);
  font-size: 14px;
  line-height: 1.5;
}

.footer-actions {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 40px;
}

.footer-actions .el-button {
  padding: 12px 24px;
  font-size: 16px;
}
</style>